home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / src / context.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-14  |  50.2 KB  |  1,826 lines

  1. /* $Id: context.c,v 1.25 1997/03/14 00:24:37 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  2.2
  6.  * Copyright (C) 1995-1997  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: context.c,v $
  26.  * Revision 1.25  1997/03/14 00:24:37  brianp
  27.  * only print runtime warnings if MESA_DEBUG env var is set
  28.  *
  29.  * Revision 1.24  1997/03/13 03:06:14  brianp
  30.  * init AlphaRefUbyte to zero
  31.  *
  32.  * Revision 1.23  1997/03/08 01:59:00  brianp
  33.  * if RenderMode != GL_RENDER then don't enable ctx->DirectTriangles
  34.  *
  35.  * Revision 1.22  1997/02/27 19:57:38  brianp
  36.  * added gl_problem() function
  37.  *
  38.  * Revision 1.21  1997/02/10 19:49:50  brianp
  39.  * added gl_ResizeBuffersMESA()
  40.  *
  41.  * Revision 1.20  1997/02/10 19:22:47  brianp
  42.  * added device driver Error() function
  43.  *
  44.  * Revision 1.19  1997/02/09 18:50:05  brianp
  45.  * added GL_EXT_texture3D support
  46.  *
  47.  * Revision 1.18  1997/01/28 22:15:14  brianp
  48.  * now there's separate state for CI and RGBA logic op enabled
  49.  *
  50.  * Revision 1.17  1997/01/09 19:56:09  brianp
  51.  * new error message format in gl_error()
  52.  *
  53.  * Revision 1.16  1997/01/09 19:47:32  brianp
  54.  * better checking and handling of out-of-memory errors
  55.  *
  56.  * Revision 1.15  1996/12/18 20:00:12  brianp
  57.  * added code to initialize ColorMaterialBitmask
  58.  *
  59.  * Revision 1.14  1996/12/11 20:18:53  brianp
  60.  * changed formatting of messages in gl_warning()
  61.  *
  62.  * Revision 1.13  1996/11/08 02:20:52  brianp
  63.  * added check for MESA_NO_RASTER env var
  64.  *
  65.  * Revision 1.12  1996/11/04 02:18:47  brianp
  66.  * multiply Viewport.Sz and .Tz by DEPTH_SCALE
  67.  *
  68.  * Revision 1.11  1996/10/16 00:52:22  brianp
  69.  * gl_initialize_api_function_pointers() now gl_init_api_function_pointers()
  70.  *
  71.  * Revision 1.10  1996/10/11 03:41:28  brianp
  72.  * removed Polygon.OffsetBias initialization
  73.  *
  74.  * Revision 1.9  1996/10/11 00:26:34  brianp
  75.  * initialize Point/Line/PolygonZoffset to 0.0
  76.  *
  77.  * Revision 1.8  1996/10/01 01:42:31  brianp
  78.  * moved device driver initialization into gl_update_state()
  79.  *
  80.  * Revision 1.7  1996/09/27 01:24:33  brianp
  81.  * removed unneeded set_thread_context(), added call to gl_init_vb()
  82.  *
  83.  * Revision 1.6  1996/09/25 03:22:53  brianp
  84.  * added NO_DRAW_BIT for glDrawBuffer(GL_NONE)
  85.  *
  86.  * Revision 1.5  1996/09/20 02:55:52  brianp
  87.  * fixed profiling bug
  88.  *
  89.  * Revision 1.4  1996/09/19 03:14:49  brianp
  90.  * now just one parameter for gl_create_framebuffer()
  91.  *
  92.  * Revision 1.3  1996/09/15 14:20:22  brianp
  93.  * added new functions for GLframebuffer and GLvisual support
  94.  *
  95.  * Revision 1.2  1996/09/14 20:12:05  brianp
  96.  * wasn't initializing a few pieces of context state
  97.  *
  98.  * Revision 1.1  1996/09/13 01:38:16  brianp
  99.  * Initial revision
  100.  *
  101.  */
  102.  
  103.  
  104. /*
  105.  * If multi-threading is enabled (-DTHREADS) then each thread has it's
  106.  * own rendering context.  A thread obtains the pointer to its GLcontext
  107.  * with the gl_get_thread_context() function.  Otherwise, the global
  108.  * pointer, CC, points to the current context used by all threads in
  109.  * the address space.
  110.  */
  111.  
  112.  
  113.  
  114. #include <assert.h>
  115. #include <math.h>
  116. #include <stdio.h>
  117. #include <stdlib.h>
  118. #include <string.h>
  119. #include "accum.h"
  120. #include "alphabuf.h"
  121. #include "context.h"
  122. #include "depth.h"
  123. #include "draw.h"
  124. #include "eval.h"
  125. #include "light.h"
  126. #include "lines.h"
  127. #include "dlist.h"
  128. #include "macros.h"
  129. #include "pb.h"
  130. #include "points.h"
  131. #include "pointers.h"
  132. #include "stencil.h"
  133. #include "triangle.h"
  134. #include "teximage.h"
  135. #include "texobj.h"
  136. #include "texture.h"
  137. #include "types.h"
  138. #include "vb.h"
  139. #include "vertex.h"
  140.  
  141.  
  142.  
  143. /**********************************************************************/
  144. /*****                  Context and Thread management             *****/
  145. /**********************************************************************/
  146.  
  147.  
  148. #ifdef THREADS
  149.  
  150. /* A different context may be bound to each thread */
  151.  
  152. #include <sys/types.h>
  153. #include <unistd.h>
  154.  
  155. #define MAX_THREADS 20
  156. static pid_t ThreadID[MAX_THREADS];
  157. static GLcontext *ThreadContext[MAX_THREADS];
  158. static int NumThreads = 0;
  159.  
  160. GLcontext *gl_get_thread_context( void )
  161. {
  162.    pid_t id;
  163.    int i;
  164.  
  165.    id = getpid();
  166.  
  167.    for (i=0;i<MAX_THREADS;i++) {
  168.       if (ThreadID[i]==id) {
  169.          return ThreadContext[i];
  170.       }
  171.    }
  172.  
  173.    /* No context for this thread */
  174.    return NULL;
  175. }
  176.  
  177. static void set_thread_context( GLcontext *ctx )
  178. {
  179.    pid_t id = getpid();
  180.    int i;
  181.  
  182.    for (i=0;i<NumThreads;i++) {
  183.       if (ThreadID[i]==id) {
  184.          ThreadContext[i] = c;
  185.          return;
  186.       }
  187.    }
  188.    if (i<MAX_THREADS) {
  189.       ThreadID[i] = getpid();
  190.       ThreadContext[i] = c;
  191.       NumThreads++;
  192.    }
  193. }
  194.  
  195. #else
  196.  
  197. /* One Current Context pointer for all threads in the address space */
  198. GLcontext *CC = NULL;
  199.  
  200. #endif /*THREADS*/
  201.  
  202.  
  203.  
  204.  
  205. /**********************************************************************/
  206. /*****                   Profiling functions                      *****/
  207. /**********************************************************************/
  208.  
  209. #ifdef PROFILE
  210.  
  211. #include <sys/times.h>
  212. #include <sys/param.h>
  213.  
  214.  
  215. /*
  216.  * Return system time in seconds.
  217.  * NOTE:  this implementation may not be very portable!
  218.  */
  219. GLdouble gl_time( void )
  220. {
  221.    static GLdouble prev_time = 0.0;
  222.    static GLdouble time;
  223.    struct tms tm;
  224.    clock_t clk;
  225.  
  226.    clk = times(&tm);
  227.  
  228. #ifdef CLK_TCK
  229.    time = (double)clk / (double)CLK_TCK;
  230. #else
  231.    time = (double)clk / (double)HZ;
  232. #endif
  233.  
  234.    if (time>prev_time) {
  235.       prev_time = time;
  236.       return time;
  237.    }
  238.    else {
  239.       return prev_time;
  240.    }
  241. }
  242.  
  243.  
  244.  
  245. /*
  246.  * Reset the timing/profiling counters
  247.  */
  248. static void init_timings( GLcontext *ctx )
  249. {
  250.    ctx->BeginEndCount = 0;
  251.    ctx->BeginEndTime = 0.0;
  252.    ctx->VertexCount = 0;
  253.    ctx->VertexTime = 0.0;
  254.    ctx->PointCount = 0;
  255.    ctx->PointTime = 0.0;
  256.    ctx->LineCount = 0;
  257.    ctx->LineTime = 0.0;
  258.    ctx->PolygonCount = 0;
  259.    ctx->PolygonTime = 0.0;
  260.    ctx->ClearCount = 0;
  261.    ctx->ClearTime = 0.0;
  262.    ctx->SwapCount = 0;
  263.    ctx->SwapTime = 0.0;
  264. }
  265.  
  266.  
  267.  
  268. /*
  269.  * Print the accumulated timing/profiling data.
  270.  */
  271. static void print_timings( GLcontext *ctx )
  272. {
  273.    GLdouble beginendrate;
  274.    GLdouble vertexrate;
  275.    GLdouble pointrate;
  276.    GLdouble linerate;
  277.    GLdouble polygonrate;
  278.    GLdouble overhead;
  279.    GLdouble clearrate;
  280.    GLdouble swaprate;
  281.    GLdouble avgvertices;
  282.  
  283.    if (ctx->BeginEndTime>0.0) {
  284.       beginendrate = ctx->BeginEndCount / ctx->BeginEndTime;
  285.    }
  286.    else {
  287.       beginendrate = 0.0;
  288.    }
  289.    if (ctx->VertexTime>0.0) {
  290.       vertexrate = ctx->VertexCount / ctx->VertexTime;
  291.    }
  292.    else {
  293.       vertexrate = 0.0;
  294.    }
  295.    if (ctx->PointTime>0.0) {
  296.       pointrate = ctx->PointCount / ctx->PointTime;
  297.    }
  298.    else {
  299.       pointrate = 0.0;
  300.    }
  301.    if (ctx->LineTime>0.0) {
  302.       linerate = ctx->LineCount / ctx->LineTime;
  303.    }
  304.    else {
  305.       linerate = 0.0;
  306.    }
  307.    if (ctx->PolygonTime>0.0) {
  308.       polygonrate = ctx->PolygonCount / ctx->PolygonTime;
  309.    }
  310.    else {
  311.       polygonrate = 0.0;
  312.    }
  313.    if (ctx->ClearTime>0.0) {
  314.       clearrate = ctx->ClearCount / ctx->ClearTime;
  315.    }
  316.    else {
  317.       clearrate = 0.0;
  318.    }
  319.    if (ctx->SwapTime>0.0) {
  320.       swaprate = ctx->SwapCount / ctx->SwapTime;
  321.    }
  322.    else {
  323.       swaprate = 0.0;
  324.    }
  325.  
  326.    if (ctx->BeginEndCount>0) {
  327.       avgvertices = (GLdouble) ctx->VertexCount / (GLdouble) ctx->BeginEndCount;
  328.    }
  329.    else {
  330.       avgvertices = 0.0;
  331.    }
  332.  
  333.    overhead = ctx->BeginEndTime - ctx->VertexTime - ctx->PointTime
  334.               - ctx->LineTime - ctx->PolygonTime;
  335.  
  336.  
  337.    printf("                          Count   Time (s)    Rate (/s) \n");
  338.    printf("--------------------------------------------------------\n");
  339.    printf("glBegin/glEnd           %7d  %8.3f   %10.3f\n",
  340.           ctx->BeginEndCount, ctx->BeginEndTime, beginendrate);
  341.    printf("  vertexes transformed  %7d  %8.3f   %10.3f\n",
  342.           ctx->VertexCount, ctx->VertexTime, vertexrate );
  343.    printf("  points rasterized     %7d  %8.3f   %10.3f\n",
  344.           ctx->PointCount, ctx->PointTime, pointrate );
  345.    printf("  lines rasterized      %7d  %8.3f   %10.3f\n",
  346.           ctx->LineCount, ctx->LineTime, linerate );
  347.    printf("  polygons rasterized   %7d  %8.3f   %10.3f\n",
  348.           ctx->PolygonCount, ctx->PolygonTime, polygonrate );
  349.    printf("  overhead                       %8.3f\n", overhead );
  350.    printf("glClear                 %7d  %8.3f   %10.3f\n",
  351.           ctx->ClearCount, ctx->ClearTime, clearrate );
  352.    printf("SwapBuffers             %7d  %8.3f   %10.3f\n",
  353.           ctx->SwapCount, ctx->SwapTime, swaprate );
  354.    printf("\n");
  355.  
  356.    printf("Average number of vertices per begin/end: %8.3f\n", avgvertices );
  357. }
  358. #endif
  359.  
  360.  
  361.  
  362.  
  363.  
  364. /**********************************************************************/
  365. /*****       Context allocation, initialization, destroying       *****/
  366. /**********************************************************************/
  367.  
  368.  
  369. /*
  370.  * Allocate and initialize a shared context state structure.
  371.  */
  372. static struct gl_shared_state *alloc_shared_state( void )
  373. {
  374.    struct gl_shared_state *ss;
  375.    struct gl_texture_object *t1, *t2, *t3;
  376.    GLuint i;
  377.  
  378.    ss = (struct gl_shared_state*) malloc( sizeof(struct gl_shared_state) );
  379.    if (!ss)
  380.       return NULL;
  381.    ss->RefCount = 0;
  382.  
  383.    /* Display lists */
  384.    for (i=0;i<MAX_DISPLAYLISTS;i++) {
  385.       ss->List[i] = NULL;
  386.    }
  387.  
  388.    /* Texture objects
  389.     * There are two default textures, one for the GL_TEXTURE_1D target and
  390.     * one for the GL_TEXTURE_2D target.  Both are named zero!
  391.     */
  392.    t1 = gl_alloc_texture_object();
  393.    if (!t1) {
  394.       free(ss);
  395.       return NULL;
  396.    }
  397.    t1->Dimensions = 1;
  398.  
  399.    t2 = gl_alloc_texture_object();
  400.    if (!t2) {
  401.       free(ss);
  402.       free(t1);
  403.       return NULL;
  404.    }
  405.    t2->Dimensions = 2;
  406.  
  407.    t3 = gl_alloc_texture_object();
  408.    if (!t3) {
  409.       free(ss);
  410.       free(t1);
  411.       free(t2);
  412.       return NULL;
  413.    }
  414.    t3->Dimensions = 3;
  415.  
  416.    ss->TexObjectList = t1;
  417.    ss->TexObjectList->Next = t2;
  418.    ss->TexObjectList->Next->Next = t3;
  419.  
  420.    return ss;
  421. }
  422.  
  423.  
  424. /*
  425.  * Deallocate a shared state context and all children structures.
  426.  */
  427. static void free_shared_state( struct gl_shared_state *ss )
  428. {
  429.    struct gl_texture_object *t, *tnext;
  430.  
  431.    /* Free the linked list of texture objects */
  432.    t = ss->TexObjectList;
  433.    while (t) {
  434.       tnext = t->Next;
  435.       gl_free_texture_object(t);
  436.       t = tnext;
  437.    }
  438.    free(ss);
  439. }
  440.  
  441.  
  442.  
  443.  
  444. /*
  445.  * Initialize the nth light.  Note that the defaults for light 0 are
  446.  * different than the other lights.
  447.  */
  448. static void init_light( struct gl_light *l, GLuint n )
  449. {
  450.    ASSIGN_4V( l->Ambient, 0.0, 0.0, 0.0, 1.0 );
  451.    if (n==0) {
  452.       ASSIGN_4V( l->Diffuse, 1.0, 1.0, 1.0, 1.0 );
  453.       ASSIGN_4V( l->Specular, 1.0, 1.0, 1.0, 1.0 );
  454.    }
  455.    else {
  456.       ASSIGN_4V( l->Diffuse, 0.0, 0.0, 0.0, 1.0 );
  457.       ASSIGN_4V( l->Specular, 0.0, 0.0, 0.0, 1.0 );
  458.    }
  459.    ASSIGN_4V( l->Position, 0.0, 0.0, 1.0, 0.0 );
  460.    ASSIGN_3V( l->Direction, 0.0, 0.0, -1.0 );
  461.    l->SpotExponent = 0.0;
  462.    gl_compute_spot_exp_table( l );
  463.    l->SpotCutoff = 180.0;
  464.    l->CosCutoff = -1.0;
  465.    l->ConstantAttenuation = 1.0;
  466.    l->LinearAttenuation = 0.0;
  467.    l->QuadraticAttenuation = 0.0;
  468.    l->Enabled = GL_FALSE;
  469. }
  470.  
  471.  
  472.  
  473. static void init_lightmodel( struct gl_lightmodel *lm )
  474. {
  475.    ASSIGN_4V( lm->Ambient, 0.2, 0.2, 0.2, 1.0 );
  476.    lm->LocalViewer = GL_FALSE;
  477.    lm->TwoSide = GL_FALSE;
  478. }
  479.  
  480.  
  481. static void init_material( struct gl_material *m )
  482. {
  483.    ASSIGN_4V( m->Ambient,  0.2, 0.2, 0.2, 1.0 );
  484.    ASSIGN_4V( m->Diffuse,  0.8, 0.8, 0.8, 1.0 );
  485.    ASSIGN_4V( m->Specular, 0.0, 0.0, 0.0, 1.0 );
  486.    ASSIGN_4V( m->Emission, 0.0, 0.0, 0.0, 1.0 );
  487.    m->Shininess = 0.0;
  488.    m->AmbientIndex = 0;
  489.    m->DiffuseIndex = 1;
  490.    m->SpecularIndex = 1;
  491.    gl_compute_material_shine_table( m );
  492. }
  493.  
  494.  
  495.  
  496. /*
  497.  * Initialize a gl_context structure to default values.
  498.  */
  499. static void initialize_context( GLcontext *ctx )
  500. {
  501.    static GLfloat identity[16] = {
  502.     1.0, 0.0, 0.0, 0.0,
  503.     0.0, 1.0, 0.0, 0.0,
  504.     0.0, 0.0, 1.0, 0.0,
  505.     0.0, 0.0, 0.0, 1.0
  506.    };
  507.    GLuint i;
  508.  
  509.    if (ctx) {
  510.       /* Transformation matrices and stacks */
  511.       ctx->ModelViewStackDepth = 0;
  512.       MEMCPY( ctx->ModelViewMatrix, identity, 16*sizeof(GLfloat) );
  513.       MEMCPY( ctx->ModelViewInv, identity, 16*sizeof(GLfloat) );
  514.       ctx->ModelViewInvValid = GL_TRUE;
  515.  
  516.       ctx->ProjectionStackDepth = 0;
  517.       MEMCPY( ctx->ProjectionMatrix, identity, 16*sizeof(GLfloat) );
  518.  
  519.       ctx->TextureStackDepth = 0;
  520.       MEMCPY( ctx->TextureMatrix, identity, 16*sizeof(GLfloat) );
  521.       ctx->IdentityTexMat = GL_TRUE;
  522.  
  523.       /* Accumulate buffer group */
  524.       ASSIGN_4V( ctx->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 );
  525.  
  526.       /* Color buffer group */
  527.       ctx->Color.IndexMask = 0xffffffff;
  528.       ctx->Color.ColorMask = 0xf;
  529.       ctx->Color.SWmasking = GL_FALSE;
  530.       ctx->Color.ClearIndex = 0;
  531.       ASSIGN_4V( ctx->Color.ClearColor, 0.0, 0.0, 0.0, 0.0 );
  532.       ctx->Color.DrawBuffer = GL_FRONT;
  533.       ctx->Color.AlphaEnabled = GL_FALSE;
  534.       ctx->Color.AlphaFunc = GL_ALWAYS;
  535.       ctx->Color.AlphaRef = 0.0;
  536.       ctx->Color.AlphaRefUbyte = 0;
  537.       ctx->Color.BlendEnabled = GL_FALSE;
  538.       ctx->Color.BlendSrc = GL_ONE;
  539.       ctx->Color.BlendDst = GL_ZERO;
  540.       ctx->Color.BlendEquation = GL_FUNC_ADD_EXT;
  541.       ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
  542.       ctx->Color.IndexLogicOpEnabled = GL_FALSE;
  543.       ctx->Color.ColorLogicOpEnabled = GL_FALSE;
  544.       ctx->Color.SWLogicOpEnabled = GL_FALSE;
  545.       ctx->Color.LogicOp = GL_COPY;
  546.       ctx->Color.DitherFlag = GL_TRUE;
  547.  
  548.       /* Current group */
  549.       ctx->Current.Index = 1;
  550.       ASSIGN_3V( ctx->Current.Normal, 0.0, 0.0, 1.0 );
  551.       ctx->Current.IntColor[0] = (GLint) ctx->Visual->RedScale;
  552.       ctx->Current.IntColor[1] = (GLint) ctx->Visual->GreenScale;
  553.       ctx->Current.IntColor[2] = (GLint) ctx->Visual->BlueScale;
  554.       ctx->Current.IntColor[3] = (GLint) ctx->Visual->AlphaScale;
  555.       ASSIGN_4V( ctx->Current.RasterPos, 0.0, 0.0, 0.0, 1.0 );
  556.       ctx->Current.RasterPosValid = GL_TRUE;
  557.       ctx->Current.RasterIndex = 1;
  558.       ASSIGN_4V( ctx->Current.TexCoord, 0.0, 0.0, 0.0, 1.0 );
  559.       ASSIGN_4V( ctx->Current.RasterColor, 1.0, 1.0, 1.0, 1.0 );
  560.       ctx->Current.EdgeFlag = GL_TRUE;
  561.  
  562.       /* Depth buffer group */
  563.       ctx->Depth.Test = GL_FALSE;
  564.       ctx->Depth.Clear = 1.0;
  565.       ctx->Depth.Func = GL_LESS;
  566.       ctx->Depth.Mask = GL_TRUE;
  567.  
  568.       /* Evaluators group */
  569.       ctx->Eval.Map1Color4 = GL_FALSE;
  570.       ctx->Eval.Map1Index = GL_FALSE;
  571.       ctx->Eval.Map1Normal = GL_FALSE;
  572.       ctx->Eval.Map1TextureCoord1 = GL_FALSE;
  573.       ctx->Eval.Map1TextureCoord2 = GL_FALSE;
  574.       ctx->Eval.Map1TextureCoord3 = GL_FALSE;
  575.       ctx->Eval.Map1TextureCoord4 = GL_FALSE;
  576.       ctx->Eval.Map1Vertex3 = GL_FALSE;
  577.       ctx->Eval.Map1Vertex4 = GL_FALSE;
  578.       ctx->Eval.Map2Color4 = GL_FALSE;
  579.       ctx->Eval.Map2Index = GL_FALSE;
  580.       ctx->Eval.Map2Normal = GL_FALSE;
  581.       ctx->Eval.Map2TextureCoord1 = GL_FALSE;
  582.       ctx->Eval.Map2TextureCoord2 = GL_FALSE;
  583.       ctx->Eval.Map2TextureCoord3 = GL_FALSE;
  584.       ctx->Eval.Map2TextureCoord4 = GL_FALSE;
  585.       ctx->Eval.Map2Vertex3 = GL_FALSE;
  586.       ctx->Eval.Map2Vertex4 = GL_FALSE;
  587.       ctx->Eval.AutoNormal = GL_FALSE;
  588.       ctx->Eval.MapGrid1un = 1;
  589.       ctx->Eval.MapGrid1u1 = 0.0;
  590.       ctx->Eval.MapGrid1u2 = 1.0;
  591.       ctx->Eval.MapGrid2un = 1;
  592.       ctx->Eval.MapGrid2vn = 1;
  593.       ctx->Eval.MapGrid2u1 = 0.0;
  594.       ctx->Eval.MapGrid2u2 = 1.0;
  595.       ctx->Eval.MapGrid2v1 = 0.0;
  596.       ctx->Eval.MapGrid2v2 = 1.0;
  597.  
  598.       /* Fog group */
  599.       ctx->Fog.Enabled = GL_FALSE;
  600.       ctx->Fog.Mode = GL_EXP;
  601.       ASSIGN_4V( ctx->Fog.Color, 0.0, 0.0, 0.0, 0.0 );
  602.       ctx->Fog.Index = 0.0;
  603.       ctx->Fog.Density = 1.0;
  604.       ctx->Fog.Start = 0.0;
  605.       ctx->Fog.End = 1.0;
  606.  
  607.       /* Hint group */
  608.       ctx->Hint.PerspectiveCorrection = GL_DONT_CARE;
  609.       ctx->Hint.PointSmooth = GL_DONT_CARE;
  610.       ctx->Hint.LineSmooth = GL_DONT_CARE;
  611.       ctx->Hint.PolygonSmooth = GL_DONT_CARE;
  612.       ctx->Hint.Fog = GL_DONT_CARE;
  613.  
  614.       /* Lighting group */
  615.       for (i=0;i<MAX_LIGHTS;i++) {
  616.      init_light( &ctx->Light.Light[i], i );
  617.       }
  618.       init_lightmodel( &ctx->Light.Model );
  619.       init_material( &ctx->Light.Material[0] );
  620.       init_material( &ctx->Light.Material[1] );
  621.       ctx->Light.ShadeModel = GL_SMOOTH;
  622.       ctx->Light.Enabled = GL_FALSE;
  623.       ctx->Light.ColorMaterialFace = GL_FRONT_AND_BACK;
  624.       ctx->Light.ColorMaterialMode = GL_AMBIENT_AND_DIFFUSE;
  625.       ctx->Light.ColorMaterialBitmask
  626.          = gl_material_bitmask( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
  627.  
  628.       ctx->Light.ColorMaterialEnabled = GL_FALSE;
  629.  
  630.       /* Line group */
  631.       ctx->Line.SmoothFlag = GL_FALSE;
  632.       ctx->Line.StippleFlag = GL_FALSE;
  633.       ctx->Line.Width = 1.0;
  634.       ctx->Line.StipplePattern = 0xffff;
  635.       ctx->Line.StippleFactor = 1;
  636.  
  637.       /* Display List group */
  638.       ctx->List.ListBase = 0;
  639.  
  640.       /* Pixel group */
  641.       ctx->Pixel.RedBias = 0.0;
  642.       ctx->Pixel.RedScale = 1.0;
  643.       ctx->Pixel.GreenBias = 0.0;
  644.       ctx->Pixel.GreenScale = 1.0;
  645.       ctx->Pixel.BlueBias = 0.0;
  646.       ctx->Pixel.BlueScale = 1.0;
  647.       ctx->Pixel.AlphaBias = 0.0;
  648.       ctx->Pixel.AlphaScale = 1.0;
  649.       ctx->Pixel.DepthBias = 0.0;
  650.       ctx->Pixel.DepthScale = 1.0;
  651.       ctx->Pixel.IndexOffset = 0;
  652.       ctx->Pixel.IndexShift = 0;
  653.       ctx->Pixel.ZoomX = 1.0;
  654.       ctx->Pixel.ZoomY = 1.0;
  655.       ctx->Pixel.MapColorFlag = GL_FALSE;
  656.       ctx->Pixel.MapStencilFlag = GL_FALSE;
  657.       ctx->Pixel.MapStoSsize = 1;
  658.       ctx->Pixel.MapItoIsize = 1;
  659.       ctx->Pixel.MapItoRsize = 1;
  660.       ctx->Pixel.MapItoGsize = 1;
  661.       ctx->Pixel.MapItoBsize = 1;
  662.       ctx->Pixel.MapItoAsize = 1;
  663.       ctx->Pixel.MapRtoRsize = 1;
  664.       ctx->Pixel.MapGtoGsize = 1;
  665.       ctx->Pixel.MapBtoBsize = 1;
  666.       ctx->Pixel.MapAtoAsize = 1;
  667.       ctx->Pixel.MapStoS[0] = 0;
  668.       ctx->Pixel.MapItoI[0] = 0;
  669.       ctx->Pixel.MapItoR[0] = 0.0;
  670.       ctx->Pixel.MapItoG[0] = 0.0;
  671.       ctx->Pixel.MapItoB[0] = 0.0;
  672.       ctx->Pixel.MapItoA[0] = 0.0;
  673.       ctx->Pixel.MapRtoR[0] = 0.0;
  674.       ctx->Pixel.MapGtoG[0] = 0.0;
  675.       ctx->Pixel.MapBtoB[0] = 0.0;
  676.       ctx->Pixel.MapAtoA[0] = 0.0;
  677.  
  678.       /* Point group */
  679.       ctx->Point.SmoothFlag = GL_FALSE;
  680.       ctx->Point.Size = 1.0;
  681.  
  682.       /* Polygon group */
  683.       ctx->Polygon.CullFlag = GL_FALSE;
  684.       ctx->Polygon.CullFaceMode = GL_BACK;
  685.       ctx->Polygon.FrontFace = GL_CCW;
  686.       ctx->Polygon.FrontMode = GL_FILL;
  687.       ctx->Polygon.BackMode = GL_FILL;
  688.       ctx->Polygon.Unfilled = GL_FALSE;
  689.       ctx->Polygon.SmoothFlag = GL_FALSE;
  690.       ctx->Polygon.StippleFlag = GL_FALSE;
  691.       ctx->Polygon.OffsetFactor = 0.0F;
  692.       ctx->Polygon.OffsetUnits = 0.0F;
  693.       ctx->Polygon.OffsetPoint = GL_FALSE;
  694.       ctx->Polygon.OffsetLine = GL_FALSE;
  695.       ctx->Polygon.OffsetFill = GL_FALSE;
  696.       ctx->Polygon.OffsetAny = GL_FALSE;
  697.  
  698.       /* Polygon Stipple group */
  699.       MEMSET( ctx->PolygonStipple, 0xff, 32*sizeof(GLuint) );
  700.  
  701.       /* Scissor group */
  702.       ctx->Scissor.Enabled = GL_FALSE;
  703.       ctx->Scissor.X = 0;
  704.       ctx->Scissor.Y = 0;
  705.       ctx->Scissor.Width = 0;
  706.       ctx->Scissor.Height = 0;
  707.  
  708.       /* Stencil group */
  709.       ctx->Stencil.Enabled = GL_FALSE;
  710.       ctx->Stencil.Function = GL_ALWAYS;
  711.       ctx->Stencil.FailFunc = GL_KEEP;
  712.       ctx->Stencil.ZPassFunc = GL_KEEP;
  713.       ctx->Stencil.ZFailFunc = GL_KEEP;
  714.       ctx->Stencil.Ref = 0;
  715.       ctx->Stencil.ValueMask = 0xff;
  716.       ctx->Stencil.Clear = 0;
  717.       ctx->Stencil.WriteMask = 0xff;
  718.  
  719.       /* Texture group */
  720.       ctx->Texture.Enabled = 0;
  721.       ctx->Texture.EnvMode = GL_MODULATE;
  722.       ASSIGN_4V( ctx->Texture.EnvColor, 0.0, 0.0, 0.0, 0.0 );
  723.       ctx->Texture.TexGenEnabled = 0;
  724.       ctx->Texture.GenModeS = GL_EYE_LINEAR;
  725.       ctx->Texture.GenModeT = GL_EYE_LINEAR;
  726.       ctx->Texture.GenModeR = GL_EYE_LINEAR;
  727.       ctx->Texture.GenModeQ = GL_EYE_LINEAR;
  728.       ASSIGN_4V( ctx->Texture.ObjectPlaneS, 1.0, 0.0, 0.0, 0.0 );
  729.       ASSIGN_4V( ctx->Texture.ObjectPlaneT, 0.0, 1.0, 0.0, 0.0 );
  730.       ASSIGN_4V( ctx->Texture.ObjectPlaneR, 0.0, 0.0, 0.0, 0.0 );
  731.       ASSIGN_4V( ctx->Texture.ObjectPlaneQ, 0.0, 0.0, 0.0, 0.0 );
  732.       ASSIGN_4V( ctx->Texture.EyePlaneS, 1.0, 0.0, 0.0, 0.0 );
  733.       ASSIGN_4V( ctx->Texture.EyePlaneT, 0.0, 1.0, 0.0, 0.0 );
  734.       ASSIGN_4V( ctx->Texture.EyePlaneR, 0.0, 0.0, 0.0, 0.0 );
  735.       ASSIGN_4V( ctx->Texture.EyePlaneQ, 0.0, 0.0, 0.0, 0.0 );
  736.  
  737.       assert( ctx->Shared->TexObjectList );
  738.       assert( ctx->Shared->TexObjectList->Next );
  739.       ctx->Texture.Current1D = ctx->Shared->TexObjectList;
  740.       ctx->Texture.Current2D = ctx->Shared->TexObjectList->Next;
  741.       ctx->Texture.Current3D = ctx->Shared->TexObjectList->Next->Next;
  742.  
  743.       /* Transformation group */
  744.       ctx->Transform.MatrixMode = GL_MODELVIEW;
  745.       ctx->Transform.Normalize = GL_FALSE;
  746.       for (i=0;i<MAX_CLIP_PLANES;i++) {
  747.      ctx->Transform.ClipEnabled[i] = GL_FALSE;
  748.          ASSIGN_4V( ctx->Transform.ClipEquation[i], 0.0, 0.0, 0.0, 0.0 );
  749.       }
  750.       ctx->Transform.AnyClip = GL_FALSE;
  751.  
  752.       /* Viewport group */
  753.       ctx->Viewport.X = 0;
  754.       ctx->Viewport.Y = 0;
  755.       ctx->Viewport.Width = 0;
  756.       ctx->Viewport.Height = 0;   
  757.       ctx->Viewport.Near = 0.0;
  758.       ctx->Viewport.Far = 1.0;
  759.       ctx->Viewport.Sx = 0.0;  /* Sx, Tx, Sy, Ty are computed later */
  760.       ctx->Viewport.Tx = 0.0;
  761.       ctx->Viewport.Sy = 0.0;
  762.       ctx->Viewport.Ty = 0.0;
  763.       ctx->Viewport.Sz = 0.5 * DEPTH_SCALE;
  764.       ctx->Viewport.Tz = 0.5 * DEPTH_SCALE;
  765.  
  766.       /* Pixel transfer */
  767.       ctx->Pack.Alignment = 4;
  768.       ctx->Pack.RowLength = 0;
  769.       ctx->Pack.SkipPixels = 0;
  770.       ctx->Pack.SkipRows = 0;
  771.       ctx->Pack.SwapBytes = GL_FALSE;
  772.       ctx->Pack.LsbFirst = GL_FALSE;
  773.       ctx->Unpack.Alignment = 4;
  774.       ctx->Unpack.RowLength = 0;
  775.       ctx->Unpack.SkipPixels = 0;
  776.       ctx->Unpack.SkipRows = 0;
  777.       ctx->Unpack.SwapBytes = GL_FALSE;
  778.       ctx->Unpack.LsbFirst = GL_FALSE;
  779.  
  780.       /* Feedback */
  781.       ctx->Feedback.Type = GL_2D;   /* TODO: verify */
  782.       ctx->Feedback.Buffer = NULL;
  783.       ctx->Feedback.BufferSize = 0;
  784.       ctx->Feedback.Count = 0;
  785.  
  786.       /* Selection/picking */
  787.       ctx->Select.Buffer = NULL;
  788.       ctx->Select.BufferSize = 0;
  789.       ctx->Select.BufferCount = 0;
  790.       ctx->Select.Hits = 0;
  791.       ctx->Select.NameStackDepth = 0;
  792.  
  793.       /* Renderer and client attribute stacks */
  794.       ctx->AttribStackDepth = 0;
  795.       ctx->ClientAttribStackDepth = 0;
  796.  
  797.       /*** Miscellaneous ***/
  798.       ctx->NewState = NEW_ALL;
  799.       ctx->RenderMode = GL_RENDER;
  800.       ctx->Primitive = GL_BITMAP;
  801.  
  802.       ctx->StippleCounter = 0;
  803.       ctx->NeedNormals = GL_FALSE;
  804.  
  805.       if (   ctx->Visual->RedScale==255.0F
  806.           && ctx->Visual->GreenScale==255.0F
  807.           && ctx->Visual->BlueScale==255.0F
  808.           && ctx->Visual->AlphaScale==255.0F) {
  809.          ctx->Visual->EightBitColor = GL_TRUE;
  810.       }
  811.       else {
  812.          ctx->Visual->EightBitColor = GL_FALSE;
  813.       }
  814.       ctx->FastDrawPixels = ctx->Visual->RGBAflag && ctx->Visual->EightBitColor;
  815.  
  816.       ctx->PointsFunc = NULL;
  817.       ctx->LineFunc = NULL;
  818.       ctx->TriangleFunc = NULL;
  819.  
  820.       ctx->CallDepth = 0;
  821.       ctx->ExecuteFlag = GL_TRUE;
  822.       ctx->CompileFlag = GL_FALSE;
  823.  
  824.       ctx->ErrorValue = GL_NO_ERROR;
  825.  
  826.       /* For debug/development only */
  827.       ctx->NoRaster = getenv("MESA_NO_RASTER") ? GL_TRUE : GL_FALSE;
  828.    }
  829. }
  830.  
  831.  
  832.  
  833. /*
  834.  * Allocate a new GLvisual object.
  835.  * Input:  rgb_flag - GL_TRUE=RGB(A) mode, GL_FALSE=Color Index mode
  836.  *         alpha_flag - alloc software alpha buffers?
  837.  *         db_flag - double buffering?
  838.  *         depth_bits - requested minimum bits per depth buffer value
  839.  *         stencil_bits - requested minimum bits per stencil buffer value
  840.  *         accum_bits - requested minimum bits per accum buffer component
  841.  *         index_bits - number of bits per pixel if rgb_flag==GL_FALSE
  842.  *         red, green, blue_scale - float-to-int color scaling
  843.  *         alpha_scale - ignored if alpha_flag is true
  844.  */
  845. GLvisual *gl_create_visual( GLboolean rgb_flag,
  846.                             GLboolean alpha_flag,
  847.                             GLboolean db_flag,
  848.                             GLint depth_bits,
  849.                             GLint stencil_bits,
  850.                             GLint accum_bits,
  851.                             GLint index_bits,
  852.                             GLfloat red_scale,
  853.                             GLfloat green_scale,
  854.                             GLfloat blue_scale,
  855.                             GLfloat alpha_scale )
  856. {
  857.    GLvisual *vis;
  858.  
  859.    /* Can't do better than 8-bit/channel color at this time */
  860.    assert( red_scale<=255.0 );
  861.    assert( green_scale<=255.0 );
  862.    assert( blue_scale<=255.0 );
  863.    assert( alpha_scale<=255.0 );
  864.  
  865.    if (depth_bits > 8*sizeof(GLdepth)) {
  866.       /* can't meet depth buffer requirements */
  867.       return NULL;
  868.    }
  869.    if (stencil_bits > 8*sizeof(GLstencil)) {
  870.       /* can't meet stencil buffer requirements */
  871.       return NULL;
  872.    }
  873.    if (accum_bits > 8*sizeof(GLaccum)) {
  874.       /* can't meet accum buffer requirements */
  875.       return NULL;
  876.    }
  877.  
  878.    vis = (GLvisual *) calloc( 1, sizeof(GLvisual) );
  879.    if (!vis) {
  880.       return NULL;
  881.    }
  882.  
  883.    vis->RGBAflag      = rgb_flag;
  884.    vis->DBflag        = db_flag;
  885.    vis->RedScale      = red_scale;
  886.    vis->GreenScale    = green_scale;
  887.    vis->BlueScale     = blue_scale;
  888.    vis->AlphaScale    = alpha_scale;
  889.    if (red_scale) {
  890.       vis->InvRedScale   = 1.0F / red_scale;
  891.    }
  892.    if (green_scale) {
  893.       vis->InvGreenScale = 1.0F / green_scale;
  894.    }
  895.    if (blue_scale) {
  896.       vis->InvBlueScale  = 1.0F / blue_scale;
  897.    }
  898.    if (alpha_scale) {
  899.       vis->InvAlphaScale = 1.0F / alpha_scale;
  900.    }
  901.    vis->IndexBits     = index_bits;
  902.    vis->DepthBits     = depth_bits;
  903.    vis->AccumBits     = accum_bits;
  904.    vis->StencilBits   = stencil_bits;
  905.  
  906.    if (red_scale==255.0F && green_scale==255.0F
  907.        && blue_scale==255.0F && alpha_scale==255.0F) {
  908.       vis->EightBitColor = GL_TRUE;
  909.    }
  910.    else {
  911.       vis->EightBitColor = GL_FALSE;
  912.    }
  913.  
  914.    /* software alpha buffers */
  915.    if (alpha_flag) {
  916.       vis->FrontAlphaEnabled = GL_TRUE;
  917.       if (db_flag) {
  918.          vis->BackAlphaEnabled = GL_TRUE;
  919.       }
  920.    }
  921.  
  922.    return vis;
  923. }
  924.  
  925.  
  926.  
  927.  
  928. void gl_destroy_visual( GLvisual *vis )
  929. {
  930.    free( vis );
  931. }
  932.  
  933.  
  934.  
  935. /*
  936.  * Allocate the proxy textures.  If we run out of memory part way through
  937.  * the allocations clean up and return GL_FALSE.
  938.  * Return:  GL_TRUE=success, GL_FALSE=failure
  939.  */
  940. static GLboolean alloc_proxy_textures( GLcontext *ctx )
  941. {
  942.    GLboolean out_of_memory;
  943.    GLint i;
  944.  
  945.    ctx->Texture.Proxy1D = gl_alloc_texture_object();
  946.    if (!ctx->Texture.Proxy1D) {
  947.       return GL_FALSE;
  948.    }
  949.  
  950.    ctx->Texture.Proxy2D = gl_alloc_texture_object();
  951.    if (!ctx->Texture.Proxy2D) {
  952.       gl_free_texture_object(ctx->Texture.Proxy1D);
  953.       return GL_FALSE;
  954.    }
  955.  
  956.    ctx->Texture.Proxy2D = gl_alloc_texture_object();
  957.    if (!ctx->Texture.Proxy2D) {
  958.       gl_free_texture_object(ctx->Texture.Proxy1D);
  959.       return GL_FALSE;
  960.    }
  961.  
  962.    ctx->Texture.Proxy3D = gl_alloc_texture_object();
  963.    if (!ctx->Texture.Proxy3D) {
  964.       gl_free_texture_object(ctx->Texture.Proxy1D);
  965.       gl_free_texture_object(ctx->Texture.Proxy2D);
  966.       return GL_FALSE;
  967.    }
  968.  
  969.    out_of_memory = GL_FALSE;
  970.    for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
  971.       ctx->Texture.Proxy1D->Image[i] = gl_alloc_texture_image();
  972.       ctx->Texture.Proxy2D->Image[i] = gl_alloc_texture_image();
  973.       ctx->Texture.Proxy3D->Image[i] = gl_alloc_texture_image();
  974.       if (!ctx->Texture.Proxy1D->Image[i]
  975.           || !ctx->Texture.Proxy2D->Image[i]
  976.           || !ctx->Texture.Proxy3D->Image[i]) {
  977.          out_of_memory = GL_TRUE;
  978.       }
  979.    }
  980.    if (out_of_memory) {
  981.       for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
  982.          if (ctx->Texture.Proxy1D->Image[i]) {
  983.             gl_free_texture_image(ctx->Texture.Proxy1D->Image[i]);
  984.          }
  985.          if (ctx->Texture.Proxy2D->Image[i]) {
  986.             gl_free_texture_image(ctx->Texture.Proxy2D->Image[i]);
  987.          }
  988.          if (ctx->Texture.Proxy3D->Image[i]) {
  989.             gl_free_texture_image(ctx->Texture.Proxy3D->Image[i]);
  990.          }
  991.       }
  992.       gl_free_texture_object(ctx->Texture.Proxy1D);
  993.       gl_free_texture_object(ctx->Texture.Proxy2D);
  994.       gl_free_texture_object(ctx->Texture.Proxy3D);
  995.       return GL_FALSE;
  996.    }
  997.    else {
  998.       return GL_TRUE;
  999.    }
  1000. }
  1001.  
  1002.  
  1003.  
  1004. /*
  1005.  * Allocate and initialize a GLcontext structure.
  1006.  * Input:  visual - a GLvisual pointer
  1007.  *         sharelist - another context to share display lists with or NULL
  1008.  *         driver_ctx - pointer to device driver's context state struct
  1009.  * Return:  pointer to a new gl_context struct or NULL if error.
  1010.  */
  1011. GLcontext *gl_create_context( GLvisual *visual,
  1012.                               GLcontext *share_list,
  1013.                               void *driver_ctx )
  1014. {
  1015.    GLcontext *ctx;
  1016.  
  1017.    /* do some implementation tests */
  1018.    assert( sizeof(GLbyte) == 1 );
  1019.    assert( sizeof(GLshort) >= 2 );
  1020.    assert( sizeof(GLint) >= 4 );
  1021.    assert( sizeof(GLubyte) == 1 );
  1022.    assert( sizeof(GLushort) >= 2 );
  1023.    assert( sizeof(GLuint) >= 4 );
  1024.  
  1025.    ctx = (GLcontext *) calloc( 1, sizeof(GLcontext) );
  1026.    if (!ctx) {
  1027.       return NULL;
  1028.    }
  1029.  
  1030.    ctx->DriverCtx = driver_ctx;
  1031.  
  1032.    ctx->Visual = visual;
  1033.    ctx->Buffer = NULL;
  1034.  
  1035.    gl_init_lists();
  1036.    gl_init_eval(ctx);
  1037.  
  1038.    ctx->VB = (struct vertex_buffer *) malloc(sizeof(struct vertex_buffer));
  1039.    if (!ctx->VB) {
  1040.       free( ctx );
  1041.       return NULL;
  1042.    }
  1043.    gl_init_vb( ctx->VB );
  1044.  
  1045.    ctx->PB = (struct pixel_buffer *) malloc(sizeof(struct pixel_buffer));
  1046.    if (!ctx->PB) {
  1047.       free( ctx->VB );
  1048.       free( ctx );
  1049.       return NULL;
  1050.    }
  1051.    PB_INIT( ctx->PB, GL_BITMAP );
  1052.  
  1053.    if (share_list) {
  1054.       /* share the group of display lists of another context */
  1055.       ctx->Shared = share_list->Shared;
  1056.    }
  1057.    else {
  1058.       /* allocate new group of display lists */
  1059.       ctx->Shared = alloc_shared_state();
  1060.       if (!ctx->Shared) {
  1061.          free(ctx->VB);
  1062.          free(ctx->PB);
  1063.          free(ctx);
  1064.          return NULL;
  1065.       }
  1066.    }
  1067.    ctx->Shared->RefCount++;
  1068.  
  1069.    initialize_context( ctx );
  1070.  
  1071.    if (visual->DBflag) {
  1072.       ctx->Color.DrawBuffer = GL_BACK;
  1073.       ctx->Pixel.ReadBuffer = GL_BACK;
  1074.    }
  1075.    else {
  1076.       ctx->Color.DrawBuffer = GL_FRONT;
  1077.       ctx->Pixel.ReadBuffer = GL_FRONT;
  1078.    }
  1079.  
  1080. #ifdef PROFILE
  1081.    init_timings( ctx );
  1082. #endif
  1083.  
  1084. #ifdef GL_VERSION_1_1
  1085.    if (!alloc_proxy_textures(ctx)) {
  1086.       free_shared_state(ctx->Shared);
  1087.       free(ctx->VB);
  1088.       free(ctx->PB);
  1089.       free(ctx);
  1090.       return NULL;
  1091.    }
  1092. #endif
  1093.  
  1094.    gl_init_api_function_pointers( ctx );
  1095.    ctx->API = ctx->Exec;   /* GL_EXECUTE is default */
  1096.  
  1097.    return ctx;
  1098. }
  1099.  
  1100.  
  1101.  
  1102.  
  1103. /*
  1104.  * Destroy a gl_context structure.
  1105.  */
  1106. void gl_destroy_context( GLcontext *ctx )
  1107. {
  1108.    if (ctx) {
  1109.       GLint i;
  1110.  
  1111. #ifdef PROFILE
  1112.       if (getenv("MESA_PROFILE")) {
  1113.          print_timings( ctx );
  1114.       }
  1115. #endif
  1116.  
  1117.       free( ctx->PB );
  1118.       free( ctx->VB );
  1119.  
  1120.       ctx->Shared->RefCount--;
  1121.       assert(ctx->Shared->RefCount>=0);
  1122.       if (ctx->Shared->RefCount==0) {
  1123.      /* free shared state */
  1124.      free_shared_state( ctx->Shared );
  1125.       }
  1126.  
  1127.       /* Free proxy texture info */
  1128.       for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
  1129.          free( ctx->Texture.Proxy1D->Image[i] );
  1130.          free( ctx->Texture.Proxy2D->Image[i] );
  1131.          free( ctx->Texture.Proxy3D->Image[i] );
  1132.       }
  1133.       free( ctx->Texture.Proxy1D );
  1134.       free( ctx->Texture.Proxy2D );
  1135.       free( ctx->Texture.Proxy3D );
  1136.  
  1137.       free( (void *) ctx );
  1138.  
  1139. #ifndef THREADS
  1140.       if (ctx==CC) {
  1141.          CC = NULL;
  1142.       }
  1143. #endif
  1144.  
  1145.    }
  1146. }
  1147.  
  1148.  
  1149.  
  1150.  
  1151. /*
  1152.  * Create a new framebuffer.  A GLframebuffer is a struct which
  1153.  * encapsulates the depth, stencil and accum buffers and related
  1154.  * parameters.
  1155.  * Input:  visual - a GLvisual pointer
  1156.  * Return:  pointer to new GLframebuffer struct or NULL if error.
  1157.  */
  1158. GLframebuffer *gl_create_framebuffer( GLvisual *visual )
  1159. {
  1160.    GLframebuffer *buffer;
  1161.  
  1162.    buffer = (GLframebuffer *) calloc( 1, sizeof(GLframebuffer) );
  1163.    if (!buffer) {
  1164.       return NULL;
  1165.    }
  1166.  
  1167.    buffer->Visual = visual;
  1168.  
  1169.    return buffer;
  1170. }
  1171.  
  1172.  
  1173.  
  1174. /*
  1175.  * Free a framebuffer struct and its buffers.
  1176.  */
  1177. void gl_destroy_framebuffer( GLframebuffer *buffer )
  1178. {
  1179.    if (buffer) {
  1180.       if (buffer->Depth) {
  1181.          free( buffer->Depth );
  1182.       }
  1183.       if (buffer->Accum) {
  1184.          free( buffer->Accum );
  1185.       }
  1186.       if (buffer->Stencil) {
  1187.          free( buffer->Stencil );
  1188.       }
  1189.       if (buffer->FrontAlpha) {
  1190.          free( buffer->FrontAlpha );
  1191.       }
  1192.       if (buffer->BackAlpha) {
  1193.          free( buffer->BackAlpha );
  1194.       }
  1195.       free(buffer);
  1196.    }
  1197. }
  1198.  
  1199.  
  1200.  
  1201.  
  1202.  
  1203. /*
  1204.  * Set the current context, binding the given frame buffer to the context.
  1205.  */
  1206. void gl_make_current( GLcontext *ctx, GLframebuffer *buffer )
  1207. {
  1208. #ifdef THREADS
  1209.    /* TODO: unbind old buffer from context? */
  1210.    set_thread_context( ctx );
  1211. #else
  1212.    if (CC && CC->Buffer) {
  1213.       /* unbind frame buffer from context */
  1214.       CC->Buffer = NULL;
  1215.    }
  1216.    CC = ctx;
  1217. #endif
  1218.  
  1219.    if (ctx && buffer) {
  1220.       /* TODO: check if ctx and buffer's visual match??? */
  1221.       ctx->Buffer = buffer;      /* Bind the frame buffer to the context */
  1222.       ctx->NewState = NEW_ALL;   /* just to be safe */
  1223.       gl_update_state( ctx );
  1224.    }
  1225. }
  1226.  
  1227.  
  1228.  
  1229. /**********************************************************************/
  1230. /*****                Miscellaneous functions                     *****/
  1231. /**********************************************************************/
  1232.  
  1233.  
  1234. /*
  1235.  * Copy attribute groups from one context to another.
  1236.  * Input:  src - source context
  1237.  *         dst - destination context
  1238.  *         mask - bitwise OR of GL_*_BIT flags
  1239.  */
  1240. void gl_copy_context( GLcontext *src, GLcontext *dst, GLuint mask )
  1241. {
  1242.    if (mask & GL_ACCUM_BUFFER_BIT) {
  1243.       MEMCPY( &dst->Accum, &src->Accum, sizeof(struct gl_accum_attrib) );
  1244.    }
  1245.    if (mask & GL_COLOR_BUFFER_BIT) {
  1246.       MEMCPY( &dst->Color, &src->Color, sizeof(struct gl_colorbuffer_attrib) );
  1247.    }
  1248.    if (mask & GL_CURRENT_BIT) {
  1249.       MEMCPY( &dst->Current, &src->Current, sizeof(struct gl_current_attrib) );
  1250.    }
  1251.    if (mask & GL_DEPTH_BUFFER_BIT) {
  1252.       MEMCPY( &dst->Depth, &src->Depth, sizeof(struct gl_depthbuffer_attrib) );
  1253.    }
  1254.    if (mask & GL_ENABLE_BIT) {
  1255.       /* no op */
  1256.    }
  1257.    if (mask & GL_EVAL_BIT) {
  1258.       MEMCPY( &dst->Eval, &src->Eval, sizeof(struct gl_eval_attrib) );
  1259.    }
  1260.    if (mask & GL_FOG_BIT) {
  1261.       MEMCPY( &dst->Fog, &src->Fog, sizeof(struct gl_fog_attrib) );
  1262.    }
  1263.    if (mask & GL_HINT_BIT) {
  1264.       MEMCPY( &dst->Hint, &src->Hint, sizeof(struct gl_hint_attrib) );
  1265.    }
  1266.    if (mask & GL_LIGHTING_BIT) {
  1267.       MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light_attrib) );
  1268.    }
  1269.    if (mask & GL_LINE_BIT) {
  1270.       MEMCPY( &dst->Line, &src->Line, sizeof(struct gl_line_attrib) );
  1271.    }
  1272.    if (mask & GL_LIST_BIT) {
  1273.       MEMCPY( &dst->List, &src->List, sizeof(struct gl_list_attrib) );
  1274.    }
  1275.    if (mask & GL_PIXEL_MODE_BIT) {
  1276.       MEMCPY( &dst->Pixel, &src->Pixel, sizeof(struct gl_pixel_attrib) );
  1277.    }
  1278.    if (mask & GL_POINT_BIT) {
  1279.       MEMCPY( &dst->Point, &src->Point, sizeof(struct gl_point_attrib) );
  1280.    }
  1281.    if (mask & GL_POLYGON_BIT) {
  1282.       MEMCPY( &dst->Polygon, &src->Polygon, sizeof(struct gl_polygon_attrib) );
  1283.    }
  1284.    if (mask & GL_POLYGON_STIPPLE_BIT) {
  1285.       MEMCPY( &dst->PolygonStipple, &src->PolygonStipple, 32*sizeof(GLuint) );
  1286.    }
  1287.    if (mask & GL_SCISSOR_BIT) {
  1288.       MEMCPY( &dst->Scissor, &src->Scissor, sizeof(struct gl_scissor_attrib) );
  1289.    }
  1290.    if (mask & GL_STENCIL_BUFFER_BIT) {
  1291.       MEMCPY( &dst->Stencil, &src->Stencil, sizeof(struct gl_stencil_attrib) );
  1292.    }
  1293.    if (mask & GL_TEXTURE_BIT) {
  1294.       MEMCPY( &dst->Texture, &src->Texture, sizeof(struct gl_texture_attrib) );
  1295.    }
  1296.    if (mask & GL_TRANSFORM_BIT) {
  1297.       MEMCPY( &dst->Transform, &src->Transform, sizeof(struct gl_transform_attrib) );
  1298.    }
  1299.    if (mask & GL_VIEWPORT_BIT) {
  1300.       MEMCPY( &dst->Viewport, &src->Viewport, sizeof(struct gl_viewport_attrib) );
  1301.    }
  1302. }
  1303.  
  1304.  
  1305.  
  1306. /*
  1307.  * This function is called when the Mesa user has stumbled into a code
  1308.  * path which may not be implemented fully or correctly.
  1309.  */
  1310. void gl_problem( GLcontext *ctx, const char *s )
  1311. {
  1312.    fprintf( stderr, "Mesa implementation error: %s\n", s );
  1313.    fprintf( stderr, "Report to Mesa author.\n" );
  1314. }
  1315.  
  1316.  
  1317.  
  1318. /*
  1319.  * This is called to inform the user that he or she has tried to do
  1320.  * something illogical or if there's likely a bug in their program
  1321.  * (like enabled depth testing without a depth buffer).
  1322.  */
  1323. void gl_warning( GLcontext *ctx, const char *s )
  1324. {
  1325.    GLboolean debug;
  1326. #ifdef DEBUG
  1327.    debug = GL_TRUE;
  1328. #else
  1329.    if (getenv("MESA_DEBUG")) {
  1330.       debug = GL_TRUE;
  1331.    }
  1332.    else {
  1333.       debug = GL_FALSE;
  1334.    }
  1335. #endif
  1336.    if (debug) {
  1337.       fprintf( stderr, "Mesa warning: %s\n", s );
  1338.    }
  1339. }
  1340.  
  1341.  
  1342.  
  1343. /*
  1344.  * This is Mesa's error handler.  Normally, all that's done is the updating
  1345.  * of the current error value.  If Mesa is compiled with -DDEBUG or if the
  1346.  * environment variable "MESA_DEBUG" is defined then a real error message
  1347.  * is printed to stderr.
  1348.  * Input:  error - the error value
  1349.  *         s - a diagnostic string
  1350.  */
  1351. void gl_error( GLcontext *ctx, GLenum error, const char *s )
  1352. {
  1353.    GLboolean debug;
  1354.  
  1355. #ifdef DEBUG
  1356.    debug = GL_TRUE;
  1357. #else
  1358.    if (getenv("MESA_DEBUG")) {
  1359.       debug = GL_TRUE;
  1360.    }
  1361.    else {
  1362.       debug = GL_FALSE;
  1363.    }
  1364. #endif
  1365.  
  1366.    if (debug) {
  1367.       char errstr[1000];
  1368.  
  1369.       switch (error) {
  1370.      case GL_NO_ERROR:
  1371.         strcpy( errstr, "GL_NO_ERROR" );
  1372.         break;
  1373.      case GL_INVALID_VALUE:
  1374.         strcpy( errstr, "GL_INVALID_VALUE" );
  1375.         break;
  1376.      case GL_INVALID_ENUM:
  1377.         strcpy( errstr, "GL_INVALID_ENUM" );
  1378.         break;
  1379.      case GL_INVALID_OPERATION:
  1380.         strcpy( errstr, "GL_INVALID_OPERATION" );
  1381.         break;
  1382.      case GL_STACK_OVERFLOW:
  1383.         strcpy( errstr, "GL_STACK_OVERFLOW" );
  1384.         break;
  1385.      case GL_STACK_UNDERFLOW:
  1386.         strcpy( errstr, "GL_STACK_UNDERFLOW" );
  1387.         break;
  1388.      case GL_OUT_OF_MEMORY:
  1389.         strcpy( errstr, "GL_OUT_OF_MEMORY" );
  1390.         break;
  1391.      default:
  1392.         strcpy( errstr, "unknown" );
  1393.         break;
  1394.       }
  1395.       fprintf( stderr, "Mesa user error: %s in %s\n", errstr, s );
  1396.    }
  1397.  
  1398.    if (ctx->ErrorValue==GL_NO_ERROR) {
  1399.       ctx->ErrorValue = error;
  1400.    }
  1401.  
  1402.    /* Call device driver's error handler, if any.  This is used on the Mac. */
  1403.    if (ctx->Driver.Error) {
  1404.       (*ctx->Driver.Error)( ctx );
  1405.    }
  1406. }
  1407.  
  1408.  
  1409.  
  1410.  
  1411. GLenum gl_GetError( GLcontext *ctx )
  1412. {
  1413.    GLenum e;
  1414.  
  1415.    if (INSIDE_BEGIN_END(ctx)) {
  1416.       gl_error( ctx, GL_INVALID_OPERATION, "glGetError" );
  1417.       return GL_INVALID_OPERATION;
  1418.    }
  1419.  
  1420.    e = ctx->ErrorValue;
  1421.    ctx->ErrorValue = GL_NO_ERROR;
  1422.    return e;
  1423. }
  1424.  
  1425.  
  1426.  
  1427. void gl_ResizeBuffersMESA( GLcontext *ctx )
  1428. {
  1429.    GLint newsize;
  1430.    GLuint buf_width, buf_height;
  1431.    
  1432.    /* ask device driver for size of output buffer */
  1433.    (*ctx->Driver.GetBufferSize)( ctx, &buf_width, &buf_height );
  1434.  
  1435.    /* see if size of device driver's color buffer (window) has changed */
  1436.    newsize = ctx->Buffer->Width!=buf_width || ctx->Buffer->Height!=buf_height;
  1437.  
  1438.    /* save buffer size */
  1439.    ctx->Buffer->Width = buf_width;
  1440.    ctx->Buffer->Height = buf_height;
  1441.  
  1442.    /* Reallocate other buffers if needed. */
  1443.    if (newsize && ctx->Visual->DepthBits>0) {
  1444.       /* reallocate depth buffer */
  1445.       (*ctx->Driver.AllocDepthBuffer)( ctx );
  1446.       (*ctx->Driver.ClearDepthBuffer)( ctx );
  1447.    }
  1448.    if (newsize && ctx->Visual->StencilBits>0) {
  1449.       /* reallocate stencil buffer */
  1450.       gl_alloc_stencil_buffer( ctx );
  1451.    }
  1452.    if (newsize && ctx->Visual->AccumBits>0) {
  1453.       /* reallocate accum buffer */
  1454.       gl_alloc_accum_buffer( ctx );
  1455.    }
  1456.    if (newsize
  1457.        && (ctx->Visual->FrontAlphaEnabled || ctx->Visual->BackAlphaEnabled)) {
  1458.       gl_alloc_alpha_buffers( ctx );
  1459.    }
  1460. }
  1461.  
  1462.  
  1463.  
  1464.  
  1465. /**********************************************************************/
  1466. /*****                   State update logic                       *****/
  1467. /**********************************************************************/
  1468.  
  1469.  
  1470. /*
  1471.  * Since the device driver may or may not support pixel logic ops we
  1472.  * have to make some extensive tests to determine whether or not
  1473.  * software-implemented logic operations have to be used.
  1474.  */
  1475. static void update_pixel_logic( GLcontext *ctx )
  1476. {
  1477.    if (ctx->Visual->RGBAflag) {
  1478.       /* RGBA mode blending w/ Logic Op */
  1479.       if (ctx->Color.ColorLogicOpEnabled) {
  1480.      if (ctx->Driver.LogicOp
  1481.              && (*ctx->Driver.LogicOp)( ctx, ctx->Color.LogicOp )) {
  1482.         /* Device driver can do logic, don't have to do it in software */
  1483.         ctx->Color.SWLogicOpEnabled = GL_FALSE;
  1484.      }
  1485.      else {
  1486.         /* Device driver can't do logic op so we do it in software */
  1487.         ctx->Color.SWLogicOpEnabled = GL_TRUE;
  1488.      }
  1489.       }
  1490.       else {
  1491.      /* no logic op */
  1492.      if (ctx->Driver.LogicOp) {
  1493.             (void) (*ctx->Driver.LogicOp)( ctx, GL_COPY );
  1494.          }
  1495.      ctx->Color.SWLogicOpEnabled = GL_FALSE;
  1496.       }
  1497.    }
  1498.    else {
  1499.       /* CI mode Logic Op */
  1500.       if (ctx->Color.IndexLogicOpEnabled) {
  1501.      if (ctx->Driver.LogicOp
  1502.              && (*ctx->Driver.LogicOp)( ctx, ctx->Color.LogicOp )) {
  1503.         /* Device driver can do logic, don't have to do it in software */
  1504.         ctx->Color.SWLogicOpEnabled = GL_FALSE;
  1505.      }
  1506.      else {
  1507.         /* Device driver can't do logic op so we do it in software */
  1508.         ctx->Color.SWLogicOpEnabled = GL_TRUE;
  1509.      }
  1510.       }
  1511.       else {
  1512.      /* no logic op */
  1513.      if (ctx->Driver.LogicOp) {
  1514.             (void) (*ctx->Driver.LogicOp)( ctx, GL_COPY );
  1515.          }
  1516.      ctx->Color.SWLogicOpEnabled = GL_FALSE;
  1517.       }
  1518.    }
  1519. }
  1520.  
  1521.  
  1522.  
  1523. /*
  1524.  * Check if software implemented RGBA or Color Index masking is needed.
  1525.  */
  1526. static void update_pixel_masking( GLcontext *ctx )
  1527. {
  1528.    if (ctx->Visual->RGBAflag) {
  1529.       if (ctx->Color.ColorMask==0xf) {
  1530.          /* disable masking */
  1531.          if (ctx->Driver.ColorMask) {
  1532.             (void) (*ctx->Driver.ColorMask)( ctx, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
  1533.          }
  1534.          ctx->Color.SWmasking = GL_FALSE;
  1535.       }
  1536.       else {
  1537.          /* Ask driver to do color masking, if it can't then
  1538.           * do it in software
  1539.           */
  1540.          GLboolean red   = (ctx->Color.ColorMask & 8) ? GL_TRUE : GL_FALSE;
  1541.          GLboolean green = (ctx->Color.ColorMask & 4) ? GL_TRUE : GL_FALSE;
  1542.          GLboolean blue  = (ctx->Color.ColorMask & 2) ? GL_TRUE : GL_FALSE;
  1543.          GLboolean alpha = (ctx->Color.ColorMask & 1) ? GL_TRUE : GL_FALSE;
  1544.          if (ctx->Driver.ColorMask
  1545.              && (*ctx->Driver.ColorMask)( ctx, red, green, blue, alpha )) {
  1546.             ctx->Color.SWmasking = GL_FALSE;
  1547.          }
  1548.          else {
  1549.             ctx->Color.SWmasking = GL_TRUE;
  1550.          }
  1551.       }
  1552.    }
  1553.    else {
  1554.       if (ctx->Color.IndexMask==0xffffffff) {
  1555.          /* disable masking */
  1556.          if (ctx->Driver.IndexMask) {
  1557.             (void) (*ctx->Driver.IndexMask)( ctx, 0xffffffff );
  1558.          }
  1559.          ctx->Color.SWmasking = GL_FALSE;
  1560.       }
  1561.       else {
  1562.          /* Ask driver to do index masking, if it can't then
  1563.           * do it in software
  1564.           */
  1565.          if (ctx->Driver.IndexMask
  1566.              && (*ctx->Driver.IndexMask)( ctx, ctx->Color.IndexMask )) {
  1567.             ctx->Color.SWmasking = GL_FALSE;
  1568.          }
  1569.          else {
  1570.             ctx->Color.SWmasking = GL_TRUE;
  1571.          }
  1572.       }
  1573.    }
  1574. }
  1575.  
  1576.  
  1577.  
  1578. /*
  1579.  * Recompute the value of ctx->RasterMask, ctx->ClipMask, etc. according to
  1580.  * the current context.
  1581.  */
  1582. static void update_rasterflags( GLcontext *ctx )
  1583. {
  1584.    ctx->RasterMask = 0;
  1585.  
  1586.    if (ctx->Color.AlphaEnabled)        ctx->RasterMask |= ALPHATEST_BIT;
  1587.    if (ctx->Color.BlendEnabled)        ctx->RasterMask |= BLEND_BIT;
  1588.    if (ctx->Depth.Test)            ctx->RasterMask |= DEPTH_BIT;
  1589.    if (ctx->Fog.Enabled)        ctx->RasterMask |= FOG_BIT;
  1590.    if (ctx->Color.SWLogicOpEnabled)    ctx->RasterMask |= LOGIC_OP_BIT;
  1591.    if (ctx->Scissor.Enabled)        ctx->RasterMask |= SCISSOR_BIT;
  1592.    if (ctx->Stencil.Enabled)        ctx->RasterMask |= STENCIL_BIT;
  1593.    if (ctx->Color.SWmasking)        ctx->RasterMask |= MASKING_BIT;
  1594.    if (ctx->Visual->FrontAlphaEnabled)    ctx->RasterMask |= ALPHABUF_BIT;
  1595.    if (ctx->Visual->BackAlphaEnabled)    ctx->RasterMask |= ALPHABUF_BIT;
  1596.  
  1597.    if (   ctx->Viewport.X<0
  1598.        || ctx->Viewport.X + ctx->Viewport.Width > ctx->Buffer->Width
  1599.        || ctx->Viewport.Y<0
  1600.        || ctx->Viewport.Y + ctx->Viewport.Height > ctx->Buffer->Height) {
  1601.       ctx->RasterMask |= WINCLIP_BIT;
  1602.    }
  1603.  
  1604.    /* check if drawing to front and back buffers */
  1605.    if (ctx->Color.DrawBuffer==GL_FRONT_AND_BACK) {
  1606.       ctx->RasterMask |= FRONT_AND_BACK_BIT;
  1607.    }
  1608.  
  1609.    /* check if writing to color buffer(s) is disabled */
  1610.    if (ctx->Color.DrawBuffer==GL_NONE) {
  1611.       ctx->RasterMask |= NO_DRAW_BIT;
  1612.    }
  1613.    else if (ctx->Visual->RGBAflag && ctx->Color.ColorMask==0) {
  1614.       ctx->RasterMask |= NO_DRAW_BIT;
  1615.    }
  1616.    else if (!ctx->Visual->RGBAflag && ctx->Color.IndexMask==0) {
  1617.       ctx->RasterMask |= NO_DRAW_BIT;
  1618.    }
  1619.  
  1620.    /* Recompute ClipMask (what has to be interpolated when clipping) */
  1621.    ctx->ClipMask = 0;
  1622.    if (ctx->Texture.Enabled) {
  1623.       ctx->ClipMask |= CLIP_TEXTURE_BIT;
  1624.    }
  1625.    if (ctx->Light.ShadeModel==GL_SMOOTH) {
  1626.       if (ctx->Visual->RGBAflag) {
  1627.      ctx->ClipMask |= CLIP_FCOLOR_BIT;
  1628.      if (ctx->Light.Model.TwoSide) {
  1629.         ctx->ClipMask |= CLIP_BCOLOR_BIT;
  1630.      }
  1631.       }
  1632.       else {
  1633.      ctx->ClipMask |= CLIP_FINDEX_BIT;
  1634.      if (ctx->Light.Model.TwoSide) {
  1635.         ctx->ClipMask |= CLIP_BINDEX_BIT;
  1636.      }
  1637.       }
  1638.    }
  1639. }
  1640.  
  1641.  
  1642.  
  1643. /*
  1644.  * If ctx->NewState is non-zero then this function MUST be called before
  1645.  * rendering any primitive.  Basically, function pointers and miscellaneous
  1646.  * flags are updated to reflect the current state of the state machine.
  1647.  */
  1648. void gl_update_state( GLcontext *ctx )
  1649. {
  1650.    if (ctx->NewState & NEW_RASTER_OPS) {
  1651.       update_pixel_logic(ctx);
  1652.       update_pixel_masking(ctx);
  1653.       update_rasterflags(ctx);
  1654.    }
  1655.  
  1656.    if (ctx->NewState & NEW_LIGHTING) {
  1657.       gl_update_lighting(ctx);
  1658.       if (ctx->Light.ColorMaterialEnabled) {
  1659.          ctx->Exec.Color4f = gl_ColorMat4f;
  1660.          ctx->Exec.Color4ub = gl_ColorMat4ub;
  1661.       }
  1662.       else {
  1663.          ctx->Exec.Color4f = gl_Color4f;
  1664.          if (ctx->Visual->EightBitColor) {
  1665.             ctx->Exec.Color4ub = gl_Color4ub8bit;
  1666.          }
  1667.          else {
  1668.             ctx->Exec.Color4ub = gl_Color4ub;
  1669.          }
  1670.       }
  1671.       if (!ctx->CompileFlag) {
  1672.          ctx->API.Color4f = ctx->Exec.Color4f;
  1673.          ctx->API.Color4ub = ctx->Exec.Color4ub;
  1674.       }
  1675.    }
  1676.  
  1677.    if (ctx->NewState & NEW_TEXTURING) {
  1678.       gl_update_texture_state(ctx);
  1679.    }
  1680.  
  1681.    if (ctx->NewState & (NEW_LIGHTING | NEW_TEXTURING)) {
  1682.       /* Check if normal vectors are needed */
  1683.       if (ctx->Light.Enabled
  1684.           || (ctx->Texture.GenModeS==GL_SPHERE_MAP
  1685.               && (ctx->Texture.TexGenEnabled & S_BIT))
  1686.           || (ctx->Texture.GenModeT==GL_SPHERE_MAP
  1687.               && (ctx->Texture.TexGenEnabled & T_BIT))) {
  1688.          ctx->NeedNormals = GL_TRUE;
  1689.       }
  1690.       else {
  1691.          ctx->NeedNormals = GL_FALSE;
  1692.       }
  1693.    }
  1694.  
  1695.    if (ctx->NewState & NEW_RASTER_OPS) {
  1696.       /* Check if incoming colors can be modified during rasterization */
  1697.       if (ctx->Fog.Enabled ||
  1698.           ctx->Texture.Enabled ||
  1699.           ctx->Color.BlendEnabled ||
  1700.           ctx->Color.SWmasking ||
  1701.           ctx->Color.SWLogicOpEnabled) {
  1702.          ctx->MutablePixels = GL_TRUE;
  1703.       }
  1704.       else {
  1705.          ctx->MutablePixels = GL_FALSE;
  1706.       }
  1707.    }
  1708.  
  1709.    if (ctx->NewState & (NEW_RASTER_OPS | NEW_LIGHTING)) {
  1710.       /* Check if all pixels generated are likely to be the same color */
  1711.       if (ctx->Light.ShadeModel==GL_SMOOTH ||
  1712.           ctx->Light.Enabled ||
  1713.           ctx->Fog.Enabled ||
  1714.           ctx->Texture.Enabled ||
  1715.           ctx->Color.BlendEnabled ||
  1716.           ctx->Color.SWmasking ||
  1717.           ctx->Color.SWLogicOpEnabled) {
  1718.          ctx->MonoPixels = GL_FALSE;       /* pixels probably multicolored */
  1719.       }
  1720.       else {
  1721.          /* pixels will all be same color,
  1722.           * only glColor() can invalidate this.
  1723.           */
  1724.          ctx->MonoPixels = GL_TRUE;
  1725.       }
  1726.    }
  1727.  
  1728.    if (ctx->NewState & NEW_RASTER_OPS) {
  1729.       /* Setup CullBits bitmask */
  1730.       ctx->Polygon.CullBits = 0;
  1731.       if (ctx->Polygon.CullFlag) {
  1732.          if (ctx->Polygon.CullFaceMode==GL_FRONT ||
  1733.              ctx->Polygon.CullFaceMode==GL_FRONT_AND_BACK) {
  1734.             ctx->Polygon.CullBits |= 1;
  1735.          }
  1736.          if (ctx->Polygon.CullFaceMode==GL_BACK ||
  1737.              ctx->Polygon.CullFaceMode==GL_FRONT_AND_BACK) {
  1738.             ctx->Polygon.CullBits |= 2;
  1739.          }
  1740.       }
  1741.       /* Any Polygon offsets enabled? */
  1742.       ctx->Polygon.OffsetAny = ctx->Polygon.OffsetPoint ||
  1743.                                ctx->Polygon.OffsetLine ||
  1744.                                ctx->Polygon.OffsetFill;
  1745.       /* reset Z offsets now */
  1746.       ctx->PointZoffset   = 0.0;
  1747.       ctx->LineZoffset    = 0.0;
  1748.       ctx->PolygonZoffset = 0.0;
  1749.    }
  1750.  
  1751.    if (ctx->NewState & (NEW_RASTER_OPS | NEW_LIGHTING)) {
  1752.       /* Determine if we can directly call the triangle rasterizer */
  1753.       if (   ctx->Polygon.Unfilled
  1754.           || ctx->Polygon.OffsetAny
  1755.           || ctx->Polygon.CullFlag
  1756.           || ctx->Light.Model.TwoSide
  1757.           || ctx->RenderMode!=GL_RENDER) {
  1758.          ctx->DirectTriangles = GL_FALSE;
  1759.       }
  1760.       else {
  1761.          ctx->DirectTriangles = GL_TRUE;
  1762.       }
  1763.    }
  1764.  
  1765.    /* update scissor region */
  1766.    ctx->Buffer->Xmin = 0;
  1767.    ctx->Buffer->Ymin = 0;
  1768.    ctx->Buffer->Xmax = ctx->Buffer->Width-1;
  1769.    ctx->Buffer->Ymax = ctx->Buffer->Height-1;
  1770.    if (ctx->Scissor.Enabled) {
  1771.       if (ctx->Scissor.X > ctx->Buffer->Xmin) {
  1772.          ctx->Buffer->Xmin = ctx->Scissor.X;
  1773.       }
  1774.       if (ctx->Scissor.Y > ctx->Buffer->Ymin) {
  1775.          ctx->Buffer->Ymin = ctx->Scissor.Y;
  1776.       }
  1777.       if (ctx->Scissor.X + ctx->Scissor.Width - 1 < ctx->Buffer->Xmax) {
  1778.          ctx->Buffer->Xmax = ctx->Scissor.X + ctx->Scissor.Width - 1;
  1779.       }
  1780.       if (ctx->Scissor.Y + ctx->Scissor.Height - 1 < ctx->Buffer->Ymax) {
  1781.          ctx->Buffer->Ymax = ctx->Scissor.Y + ctx->Scissor.Height - 1;
  1782.       }
  1783.    }
  1784.  
  1785.    /*
  1786.     * Update Device Driver interface
  1787.     */
  1788.    if (ctx->NewState & NEW_RASTER_OPS) {
  1789.       ctx->Driver.AllocDepthBuffer = gl_alloc_depth_buffer;
  1790.       ctx->Driver.ClearDepthBuffer = gl_clear_depth_buffer;
  1791.       if (ctx->Depth.Mask) {
  1792.          switch (ctx->Depth.Func) {
  1793.             case GL_LESS:
  1794.                ctx->Driver.DepthTestSpan = gl_depth_test_span_less;
  1795.                ctx->Driver.DepthTestPixels = gl_depth_test_pixels_less;
  1796.                break;
  1797.             case GL_GREATER:
  1798.                ctx->Driver.DepthTestSpan = gl_depth_test_span_greater;
  1799.                ctx->Driver.DepthTestPixels = gl_depth_test_pixels_greater;
  1800.                break;
  1801.             default:
  1802.                ctx->Driver.DepthTestSpan = gl_depth_test_span_generic;
  1803.                ctx->Driver.DepthTestPixels = gl_depth_test_pixels_generic;
  1804.          }
  1805.       }
  1806.       else {
  1807.          ctx->Driver.DepthTestSpan = gl_depth_test_span_generic;
  1808.          ctx->Driver.DepthTestPixels = gl_depth_test_pixels_generic;
  1809.       }
  1810.       ctx->Driver.ReadDepthSpanFloat = gl_read_depth_span_float;
  1811.       ctx->Driver.ReadDepthSpanInt = gl_read_depth_span_int;
  1812.    }
  1813.    if (ctx->Driver.UpdateState) {
  1814.       (*ctx->Driver.UpdateState)(ctx);
  1815.    }
  1816.  
  1817.    gl_set_point_function(ctx);
  1818.    gl_set_line_function(ctx);
  1819.    gl_set_triangle_function(ctx);
  1820.  
  1821.    gl_set_vertex_function(ctx);
  1822.  
  1823.    ctx->NewState = 0;
  1824. }
  1825.  
  1826.